import re

from utils.fake.addr import Address
from utils.fake.fakeValue import fkValue, fake


class fakeUser():

    def __init__(self,idcard=None):
        #     self.initdata()
        #
        # def initdata(self):
        while 1:  # 直到身份证 地址正确
            try:
                self.genByIDcard(fkValue().ssn(min_age=20, max_age=45)
                                 if idcard is None else idcard)
                break
            except:
                print('idcard err, regenerate. %s' % (self.idcard))
                idcard = fkValue().ssn(min_age=20, max_age=45)
                pass

        self.phone = fake.phone_number()
        self.bankcard = fake.bankCard('62220238')
        self.company = fake.company()

    def genByIDcard(self,idcard):
        self.idcard = idcard
        self.setAddress(Address(idcard))

        nanv = int(self.idcard[16])%2    # 0=女  1=男
        self.sex = ['女','男'][nanv]
        self.name = self.getUserName(nanv)

        # 第三级区名大于三个字，减去最后一个字
        self.org = self.city+"公安局"+\
                   (self.county[:len(self.county)-1] if len(self.county)>=3 else self.county) +"分局"

    def initByInfo(self,info):
        #   解析info 字段  ， 姓名、身份证
        # re.split(r"[.。!！?？；;，,\s+]",
        #          "手机号:18849215219,姓名:林柳,身份证:620600197711166614,用户 ID:1124968782968918016,银行卡 : 6222 0238 1945 4112 475,orderID：95ddf294-d3fb-4616-85e1-27805c0f39c2")

        result_dict={}

        for data_str in re.split(r"[。!！|；;，,]",info):
            # 取出'X：X'形式的字符串，按冒号切割为列表，
            # 再取第一个值做为key，第二个做为value，赋值给空字典
            xx = re.split(r"[：:＝=]",data_str)
            if len(xx)>1 :
               result_dict[xx[0]] = xx[1]


        name =result_dict['姓名']
        idcard =result_dict['身份证']



        self.genByIDcard(idcard)
        self.name = name


    def setAddress(self,oAddr):
        # df = transform([str], cut=False)

        self.addr = oAddr.get_addr()
        self.province= oAddr.get_shenfen()
        self.city= oAddr.get_city()
        self.county= oAddr.get_county()

    def getUserName(self,sex =1):
        if sex==1 :
            return fkValue().last_name()+fkValue().first_name_male()
        else:
            return fkValue().last_name()+fkValue().first_name_female()



    def __str__(self):

        return self.name+':'+self.sex+':'+self.idcard+':'+self.province+':'+self.city+':'+self.county+':'+self.addr

    def __dict__(self):
        user = {}
        user['name'] = self.name
        user['phone'] = self.phone
        user['idcard'] = self.idcard
        user['bankcard'] = self.bankcard
        user['addr'] = self.addr
        user['company'] = self.company
        user['sex'] = self.sex
        user['org'] = self.org
        return dict(user)


if __name__ == "__main__":
    print(fakeUser().__dict__())
